/*
编写 : Hexh
时间 : 2012-6-13 16:02:26
*/
using System;
using System.Collections.Generic;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository;
using HMIS.FrameWork.Repository.Nhb;
using FsCarInfo;
using HMIS.Infrastructure.Core.Repositories.Impl;
using HMIS.Infrastructure.Core.Entities;

namespace FsCarInfo.Core.Entity.IRespositories.Impl
{
	/// <summary>
	///	 the repository of FsCartransfer
	/// </summary>
	public class FsCartransferRepositoryImpl : RepositoryNhbImpl<FsCartransfer>, IFsCartransferRepository
	{

		/// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <returns>True， False</returns>
		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}
		
		// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <param name="where">Where 条件</param>
		/// <returns>True， False</returns>
		public bool IsFieldExistWhere(string fieldName, string fieldValue, string id, string where)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, where);
		}
	
		/// <summary>
		/// 查询 FsCartransfer
		/// </summary>
		/// <param name="query">查询条件</param>
		/// <param name="start">分页开始</param>
		/// <param name="limit">分页大小</param>
		/// <param name="sort">排序条件</param>
		/// <param name="dir">排序方向</param>
		/// <param name="filters">筛选条件</param>
		/// <param name="total">返回记录条数</param>
		/// <returns>返回记录集</returns>
		public IList<FsCartransfer> GetPlist(string state, string type, string deptId, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "t");
			
			// 修改查询条件
			string strQuery = string.IsNullOrEmpty(state) ? strFilter
				: "( t.State = :state) " 
				+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter);
			// 调拨类型条件
			strQuery = string.IsNullOrEmpty(type) ? strQuery
				: "( t.TransferType = :type) "
				+ (string.IsNullOrEmpty(strQuery) ? string.Empty : " and " + strQuery);
			// 调拨部门条件
			strQuery = string.IsNullOrEmpty(deptId) ? strQuery
				: "( t.OutDeptId = :deptId or t.InDeptId = :deptId )"
				+ (string.IsNullOrEmpty(strQuery) ? string.Empty : " and " + strQuery);

			// sort
			string strSort = string.IsNullOrEmpty(sort) ? string.Empty : " order by t." + sort + " " + dir;

			// todo : 修改关联查询条件 OuterColumn
			string strSql = @" from FsCartransfer as t 
									left join fetch t.OperUser
									left join fetch t.OutDept
									left join fetch t.OutDpuser
									left join fetch t.InDept 
									left join fetch t.InDpuser "
				+ (string.IsNullOrEmpty(strQuery) ? string.Empty : " where " + strQuery);

			// 查询
			var selQuery = Session.CreateQuery(@"select t " + strSql + strSort);
			var totQuery = Session.CreateQuery(@"select count(*) " + strSql.Replace(" join fetch ", " join "));

			// 设置查询条件
			if (!string.IsNullOrEmpty(state))
			{
				selQuery.SetString("state", state);
				totQuery.SetString("state", state);
			}
			if (!string.IsNullOrEmpty(type))
			{
				selQuery.SetString("type", type);
				totQuery.SetString("type", type);
			}
			if (!string.IsNullOrEmpty(deptId))
			{
				selQuery.SetString("deptId", deptId);
				totQuery.SetString("deptId", deptId);
			}

			// 分页
			if (limit > 0)
				selQuery.SetFirstResult(start).SetMaxResults(limit);

			// 汇总并返回
			total = totQuery.UniqueResult<long>();
			return selQuery.List<FsCartransfer>();
		}

		/// <summary>
		/// 返回超时的用车信息
		/// </summary>
		/// <returns></returns>
		public IList<FsCartransfer> GetOverTimePlist()
		{
			// 修改关联查询条件 OuterColumn
			string strSql = @"select t from FsCartransfer as t 
								where t.State = :State and t.ExpArvTime < :currentTime and t.SmsTime < :SMSTIME";
			// 查询
			var selQuery = Session.CreateQuery(strSql);
			// 条件
			selQuery.SetParameter("State", FsCartransferState.Processing);
			selQuery.SetDateTime("currentTime", System.DateTime.Now);
			// 设置发送间隔
			selQuery.SetDateTime("SMSTIME", System.DateTime.Now.AddMinutes(-30));

			// 返回
			return selQuery.List<FsCartransfer>();
		}

		/// <summary>
		/// 扫描超时记录并发送短信
		/// </summary>
		public void SendOverTimeSms()
		{
			InfraSmsRepositoryImpl smsRep = new InfraSmsRepositoryImpl();
			InfraApproveLogRepositoryImpl apprlogRep = new InfraApproveLogRepositoryImpl();

			IList<FsCartransfer> otList = this.GetOverTimePlist();
			foreach (FsCartransfer appl in otList)
			{
				long total;
				string smsContent = "[" + appl.TransferType + "超时警告!]\n" + appl.ToSmsString();
				List<string> sendList = new List<string>();
				// 发送申请人、调拨仓管和审批人
				sendList.Add(appl.OperUser.Mobitl);

				IList<InfraApproveLog> approveList = apprlogRep.GetPlist(appl.Id,"", 0, 0, "", "", null, out total);
				foreach (InfraApproveLog logstep in approveList)
				{
					if (sendList.IndexOf(logstep.User.Mobitl) < 0)
						sendList.Add(logstep.User.Mobitl);
				}
				foreach (string mobile in sendList)
				{
					if (!String.IsNullOrEmpty(mobile))
					{
						smsRep.SendSms(appl.Id, mobile, smsContent);
						System.Threading.Thread.Sleep(200);	//等待200毫秒再发下一条
					}
				}

				// 更新最近发送短信的时间
				appl.SmsTime = System.DateTime.Now;
				SaveOrUpdate(appl);
			}
		}
	}
}
